<?php
namespace Admin\Controller;

use Think\Page;

class ThemeController extends CommonController
{
    public $model = null;

    public function __construct()
    {
        parent::__construct();
        $this->model = M('theme');
    }

    /**
     * 数据列表
     * @return [type]
     */
    public function index()
    {
        $where = ['is_delete' => 0];

        // 查找主题名称
        $name = I('get.name', '', 'trim');
        if ($name) {

            $where['name']  = ['LIKE', '%' . $name . '%'];
            $fields['name'] = $name;
            $this->assign('name', $name);
        }

        $count = $this->model->where($where)->count();
        $this->assign('count', $count);

        $pages = new Page($count, C('PAGE'), $fields);
        $this->assign('pages', $pages->show());

        $datas = $this->model->where($where)
            ->order('sort desc, id desc')
            ->limit($pages->firstRow . ',' . $pages->listRows)
            ->select();
        $this->assign('datas', $datas);

        $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
            '推广设置',
            '主题',
        ]));
        $this->display();
    }

    /**
     * 添加数据，添加完修改
     */
    public function add()
    {
        if (IS_GET) {

            $this->display();

        } elseif (IS_POST) {
            $data = [
                'name'        => I('post.name', '', 'trim'),
                'description' => I('post.description', '', 'trim'),
                'sort'        => I('post.sort', 0, 'intval'),
            ];

            $result = upload('Theme_Image');
            var_dump($_FILES);exit;
            var_dump($result);exit;
            $data['add_time'] = CUR_DATETIME;
            $id               = $this->model->add($data);

            if (isset($result['image'])) {
                $this->model
                    ->where(['id' => $id])
                    ->save(['image' => C('UPLOAD_DOMAIN') . $result['image']['url']]);
            }

            if ($id) {
                $this->success('添加成功', U(CONTROLLER_NAME . '/update', [
                    'id' => $id,
                ]));
            }
        }
    }

    public function update()
    {
        if (IS_GET) {

            $id = I('get.id', 0, 'intval');

            $data = $this->model->find($id);
            $this->assign('data', $data);

            $this->display();

        } elseif (IS_POST) {

            $id   = I('post.id', 0, 'intval');
            $data = [
                'name'        => I('post.name', '', 'trim'),
                'description' => I('post.description', '', 'trim'),
                'sort'        => I('post.sort', 0, 'intval'),
            ];

            $this->model->where(['id' => $id])->save($data);

            $result = upload('Theme_Image');

            if (isset($result['image'])) {
                $this->model
                    ->where(['id' => $id])
                    ->save(['image' => C('UPLOAD_DOMAIN') . $result['image']['url']]);
            }

            $this->success('修改成功', U(CONTROLLER_NAME . '/update', [
                'id' => $id,
            ]));
        }
    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function delete()
    {
        $id = I('post.id');

        $is = $this->model
            ->where(['id' => ['in', explode(',', $id)]])
            ->save([
                'is_delete'   => 1,
                'delete_time' => CUR_DATETIME,
            ]);

        if ($is) {
            success([
                'id' => $id,
            ]);
        } else {
            error(101, '删除失败');
        }
    }
    /**
     * 主题商品
     * @return [type]
     */
    public function goods()
    {
        $id    = I('get.id', 0, 'intval');
        $where = [
            'is_delete' => 0,
            'goods'     => $id,
        ];

        // 查找主题名称
        $name = I('get.name', '', 'trim');
        if ($name) {

            $where['name']  = ['LIKE', '%' . $name . '%'];
            $fields['name'] = $name;
            $this->assign('name', $name);
        }

        $count = $this->model->where($where)->count();
        $this->assign('count', $count);

        $pages = new Page($count, C('PAGE'), $fields);
        $this->assign('pages', $pages->show());

        $datas = $this->model->where($where)
            ->order('sort desc, id desc')
            ->limit($pages->firstRow . ',' . $pages->listRows)
            ->select();
        $this->assign('datas', $datas);

        $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
            '推广设置',
            '主题',
        ]));
        $this->display();
    }
}
